﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ECSS.PaymentProvider;

namespace Microsoft.ECSS.Payment.PaymentTradeAccount
{
    public class TradeAccountReportFactory
    {
        /// <summary>
        /// 实时对账（即时从支付渠道查询交易信息进行对账）
        /// </summary>
        /// <param name="providerCode"></param>
        /// <returns></returns>
        public static TradeAccountEntity RealtimeTradeAccountReport(string providerCode, PayChannelInterface payItf)
        {
            TradeAccountEntity res = new TradeAccountEntity();
            try
            {
                //对账功能的契约
                ITradeAccountReportProvider provider = null;
                switch (providerCode)
                {
                    //case "AlipayNet": provider = new AlipayTradeAccountReportProvider(); break;
                    case "CMBTel": provider = new CMBTradeAccountReportProvider(); break;
                    case "UnionPay": provider = new UnionPayTradeAccountReportProvider(); break;
                    case "CMBOnlinePay": provider = new CMBOnlineTradeAccountReportProvider(); break;
                    default: break;
                }
                if (provider != null)
                {
                    try
                    {
                        //查询支付平台的交易记录
                        res = provider.Search(payItf);

                        // 记录接收日志
                        Microsoft.ECSS.Common.ECSSLogger.PaymentLogger.PaymentInterfaceLog(new Microsoft.ECSS.Common.Entity.PaymentInterfaceLog()
                        {
                            InterfaceName = "自动对账",
                            IsSuccess = 0,
                            Sender = providerCode == "UnionPay" ? "银联" : "招行",
                            Receiver = "支付网关",
                            DealTime = DateTime.Now,
                            RelationKey = providerCode,
                            RequestContent = String.Format("PayChannelInterface={0}", Microsoft.ECSS.Common.Function.XmlObjectHelper.ObjectToXML(payItf)),
                            ResponseContent = Microsoft.ECSS.Common.Function.XmlObjectHelper.ObjectToXML(res)
                        });
                    }
                    catch (Exception ex)
                    {
                        res.Result = 1;
                        res.Message = String.Format("{0}查询对账失败：{1}", providerCode, ex.Message);

                        //记录异常日志
                        Microsoft.ECSS.Common.ECSSLogger.PaymentLogger.ExceptionLog("自动对账", "Microsoft.ECSS.ECSSOuterWinService.ECSSOuterWinServiceHost.TradeAccountReport", "对账失败-" + providerCode, "-1", ex, false);
                    }
                }
                else
                {
                    res.Result = 1;
                    res.Message = String.Format("{0}对账接口未实现", providerCode);
                }
            }
            catch (Exception ex)
            {
                res.Result = 1;
                res.Message = ex.Message;

                //记录异常日志
                Microsoft.ECSS.Common.ECSSLogger.PaymentLogger.ExceptionLog("自动对账", "Microsoft.ECSS.ECSSOuterWinService.ECSSOuterWinServiceHost.TradeAccountReport", "对账失败-" + providerCode, "-1", ex, false);
            }
            return res;
        }
    }
}
